﻿

namespace NCP.Recruiting.Domain.Aggregates.Companies
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NCP.Infrastructure.Common.Domain;
    using NCP.Recruiting.Domain.Aggregates.Jobpositions;
    using NCP.Recruiting.Domain.Aggregates.Contracts;
    using NCP.Recruiting.Domain.Shared;

    public class Company : Entity
    {
        //private ICollection<JobPosition> _jobpositions;
        //private ICollection<Contract> _contracts;

        //public Company()
        //{
        //    _jobpositions = new List<JobPosition>();
        //    _contracts = new List<Contract>();
        //}

        public string CompanyName { get; set; }
        public string AnotherName { get; set; }
        public string ShortName { get; set; }
        public string Shortcut { get; set; }
        public int? Industry { get; set; }
        public int? CompanyType { get; set; }
        public int? Employees { get; set; }
        public string BusinessLicense { get; set; }
        public int? RegisteredCapital { get; set; }
        public DateTime? EstablishedOn { get; set; }
        public string Overview { get; set; }
        public string Business { get; set; }
        public int? Province { get; set; }
        public int? City { get; set; }
        public string Address { get; set; }
        public string PostalCode { get; set; }
        public string Contact { get; set; }
        public string Tel { get; set; }
        public string Fax { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
        public string WebSite { get; set; }
        public int? Park { get; set; }
        public byte[] Logo { get; set; }
        public bool? IsGroup { get; set; }
        public Guid? ParentCompanyId { get; set; }
        public bool? EnableShortName { get; set; }
        public bool? EnableAnotherName { get; set; }
        public DateTime? DailySigned { get; set; }
        public string Template { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string PasswordSalt { get; set; }
        public DateTime DateCreated { get; set; }
        public DateTime LastUpdated { get; set; }
        public bool Deleted { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public virtual ICollection<JobPosition> Jobpositions
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual ICollection<Contract> Contracts
        {
            get;
            set;
        }


        // 测试用
        public int HasPostedPositions
        {
            get
            {
                var postings = Jobpositions.Where(j => j.Status == 1).ToList();
                return postings.Count;
            }
        }

        public bool IsValidMember
        {
            get
            {
                var contract = Contracts.Where(c => c.Status == 1 & c.EndDate >= DateTime.Today).SingleOrDefault();
                if (contract != null)
                    return true;
                return false;
            }
        }

        /// <summary>
        /// 剩余数量
        /// </summary>
        public int RemainingQuantity
        {
            get
            {
                int remainingQuantity = 0;
                var contract = Contracts.Where(c => c.Status == 1 & c.EndDate >= DateTime.Today).FirstOrDefault();
                if (contract != null)
                {
                    switch (contract.MemberLevel)
                    {
                        case (int)MemberLevel.Premium:
                            remainingQuantity = 9999;
                            break;
                        case (int)MemberLevel.New:
                            remainingQuantity = 0;
                            break;
                        case (int)MemberLevel.General:
                        case (int)MemberLevel.Recruitment:
                            var postedCount = Jobpositions.Where(x => x.Status == (int)JobPositionStatus.Active && !x.Deleted && x.EndDate >= DateTime.Today).Count();
                            remainingQuantity = contract.Positions - postedCount;
                            break;
                    }
                }
                return remainingQuantity;
            }
        }

        /// <summary>
        /// 允许使用部门地址
        /// </summary>
        /// <returns></returns>
        public bool IsAllowUseDepartmentAddress
        {
            get
            {
                var contract = Contracts.Where(c => c.Status == 1 & c.EndDate >= DateTime.Today).FirstOrDefault();
                if (contract != null)
                {
                    return contract.MemberLevel == (int)MemberLevel.Premium;
                }
                return false;
            }
        }

        /// <summary>
        /// 是否允许发布职位
        /// </summary>
        /// <param name="postCount">发布的个数</param>
        /// <returns></returns>
        public bool AllowedPostJobposition(int postCount = 1)
        {
            return postCount <= RemainingQuantity;
        }
    }
}

